<?php 
if(KT!='KhiThe') exit;
class modules_controller extends modules_model{
		
	public function __construct(){
		global $lang;
		$this->lang = $lang;
		$this->lang['sms'] ='';
		parent::__construct();
		parent::addObject('khithe_template');
		parent::addObject('khithe_string');
		parent::addObject('khithe_request');
		parent::addObject('khithe_upload');
	}
	
	public function index_action()
	{
		$strHtmlContent = $this->khithe_display('index');
		$arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
		$selected = '';
		$resultHtml = '';
		$resultmodule = parent::getmodules();
		while( $row_modules = mysql_fetch_assoc($resultmodule) )
		{
				
			$arrmodule = array(
				"cell_ID"=>$row_modules['ID'],
				"cell_Value"=>isset($row_modules['ModuleName'])?$row_modules['ModuleName']:NULL,
				"cell_selected"=>$selected==$row_modules['ID']?' selected ':'',
				"cell_1"=>'<input name="delId[]" type="checkbox" value="'.$row_modules['ID'].'" />',
				"cell_2"=>$row_modules['ModuleDir'],
				"cell_3"=>$row_modules['ModuleTitle'],
				"cell_4"=>$row_modules['AppName'],
				"cell_5"=>$row_modules['Status']=='true'?$this->lang['enabled']:$this->lang['disabled'],
				"class_cell"=>$row_modules['Status']=='true'?"cell_groupitem1":"cell_groupitem_ex1",
				
			);
			
			$resultHtml .= $this->khithe_assign($arrBlockHtml['modules'],$arrmodule);
			
		} mysql_free_result($resultmodule);
		
		$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlCat'=>$resultHtml));
		$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
		return $strHtmlContent;
	}
	
	public function add_action()
	{
		if($_GET['iframe']=='true')
		{
			if(isset($_POST['txt_modules']))
			{
				$_POST['Picture'] = $this->upload_img($_FILES['txt_Picmodule'],path_base.'/uploads/modules/',1000000);					
				$modulename = parent::checkStrMySQL($_POST['txt_modules']);					
				if($modulename==''){
					$this->language['sms'] = $this->language['Emptymodule'];
				
				}else{
					$done = parent::updateInsertmodule($_POST);
					if($done) parent::gotowinparent(url_base.'admin/modules/');
				}
			}
			$this->lang['txt_modulesNameValue'] =$_POST['txt_modulesName'];
			$this->lang['txt_modulesValue'] =$_POST['txt_modules'];
			$this->lang['select_admin'] =($_POST['txt_app_module']=='admin'?' selected="selected" ':'');
			$this->lang['select_publics'] =($_POST['txt_app_module']=='publics'?' selected="selected" ':'');
		
			$this->lang['active'] ='add';
			$this->lang['modulesID'] ='';
			$this->lang['titlePageEditor'] = $this->lang['addTitle'];
			$strHtmlContent = $this->khithe_display('editor');
			$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
			return $strHtmlContent;
		}
		
	}
	
	public function edit_action()
	{
		if($_GET['iframe']=='true')
		{
			if(isset($_POST['txt_modules']))
			{
				$_POST['Picture'] = $this->upload_img($_FILES['txt_Picmodule'],path_base.'/uploads/modules/',1000000);					
				$modulename = parent::checkStrMySQL($_POST['txt_modules']);					
				if($modulename==''){
					$this->language['sms'] = $this->language['Emptymodule'];
				
				}else{
					$done = parent::updateInsertmodule($_POST);

					if($this->valUrl(2)=='PositionModules'){
                        parent::gotowinparent(url_base.'admin/PositionModules/index/'.$this->valUrl(3));
                        exit();
                    }
                    if($done) parent::gotowinparent(url_base.'admin/modules');
				}
			}
			$resultmodule = parent::getmoduleID($this->valUrl(1));
			$row_modules = mysql_fetch_assoc($resultmodule);
			$strHtmlContent = $this->khithe_display('editor');
			$this->lang['titlePageEditor'] = $this->lang['editTitle'];
			$this->lang['txt_modulesNameValue'] = $_POST['txt_modulesName']!=''?$_POST['txt_modulesName']:$row_modules['ModuleTitle'];
			$this->lang['txt_modulesValue'] = $_POST['txt_modules']!=''?$_POST['txt_modules']:$row_modules['ModuleDir'];
			$this->lang['select_admin'] =($row_modules['AppName']=='admin' || $_POST['txt_app_module']=='admin'?' selected="selected" ':'');
			$this->lang['select_publics'] =($row_modules['AppName']=='publics' || $_POST['txt_app_module']=='publics'?' selected="selected" ':'');
			
			$this->lang['active'] ='edit';
			$this->lang['modulesID'] =$row_modules['ID'];

			$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
			return $strHtmlContent;
		}
		
	}
	
	public function index_ajax_action()
	{
		switch ($this->valUrl(1))
		{
			case 'open':
				$arr = explode(',',$this->valUrl(2));
				foreach ( $arr as $ID ){
					$select = parent::getmoduleID($ID);
					while($row = mysql_fetch_assoc($select)){
						if($row['Status']=='false'){
							$done = parent::setCheckedmodule($ID,'true');
						}
					}
				}
			break;
			
			case 'close':
				$arr = explode(',',$this->valUrl(2));
				foreach ( $arr as $ID ){
					$select = parent::getmoduleID($ID);
					while($row = mysql_fetch_assoc($select)){
						if($row['Status']=='true'){
							$done = parent::setCheckedmodule($ID,'false');
						}
					}
				}
			break;
			
			case 'del':
				$arr = explode(',',$this->valUrl(2));
				foreach ( $arr as $ID ){
					$done = parent::delCheckedmodule($ID);	
				}
			break;
			
		}
		if($this->valUrl(1)!=''&&isset($done)){
			return $this->index_action();
			
		}else{
			return 'notOK';
		}
	}


}
?>